next up previous
Next: 4.7 Extension Writers Beware Up: 4 The Effects of Previous: 4.5 Careful with X

4.6 Xt Argument Lists

The X Toolkit has two programmatic interfaces to setting and getting widget resource values. Both interfaces circumvent the compiler's ability to catch type mismatches and are therefore prone to 64-bit portability problems, particularly when getting resources. The older interface sets and gets resource values through the construction of ArgList resource templates. These templates allow no opportunity for compiler type checking because resource values in the argument lists are considered having dynamic type.

The example below works fine on a 32-bit system and will compile without errors on a 64-bit system, but the 64-bit executable has a possibly fatal bug:

#include <Xm/Xm.h>
#include <Xm/Label.h>
main(int argc, char **argv)
{
  XtAppContext app_context;
  /* pixel should be of type long! */
  int padding_needed_to_demo_bug, pixel;
  Widget toplevel, hello;
  Arg arg;

  toplevel = XtVaAppInitialize(
    &app_context, "xhello", NULL, 0,
    &argc, argv, NULL, NULL);
  hello = XtVaCreateManagedWidget(
    "hello", xmLabelWidgetClass,
    toplevel, NULL);
  XtSetArg(arg, XmNbackground, &pixel);
  XtGetValues(hello, &arg, 1);
  printf("pixel = %ld\n", pixel);
  XtRealizeWidget(toplevel);
  XtAppMainLoop(app_context);
}
Compiled as a 64-bit executable on an IRIX 6.0.1 machine, the program incorrectly outputs zero and corrupts the word of memory following the pixel variable. One observed result is the corruption of the toplevel widget value, crashing the program. If the pixel variable is declared to be of type Pixel, the program works as expected. The unmodified program incorrectly assumes that the X Toolkit's Pixel type is the same size as an int.

The second, newer interface is potentially more prone to errors because it relies on variable argument lists. The program above would also fail in the same way if the XtSetArg and XtGetValues lines were replaced with:

  XtVaGetValues(hello,
    XmNbackground, &pixel, NULL);

Widget resources of type Pixel, XawTextPosition, and XmTextPosition can be easily used incorrectly as in the above example. Be careful since the underlying type for each of these types is long, not int.



next up previous
Next: 4.7 Extension Writers Beware Up: 4 The Effects of Previous: 4.5 Careful with X



Mark Kilgard
Sat Dec 30 11:52:07 PST 1995